package com.manong.web.controller;

import com.github.pagehelper.PageInfo;
import com.manong.data.ResultData;
import com.manong.domain.Permission;
import com.manong.query.QueryObject;
import com.manong.service.IPermissionService;
import com.manong.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/permission")
@CrossOrigin(allowCredentials = "true")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    /**
     * 分页查询接口
     * @param queryObject
     * @return
     */
    @GetMapping("/list")
    public JsonResult list(QueryObject queryObject) {
        PageInfo pageInfo = permissionService.query(queryObject);
        // 先解决JsonResult->Data属性解决
        ResultData data = new ResultData(queryObject.getPageNum(), queryObject.getPageSize(),pageInfo.getList(),Integer.parseInt(pageInfo.getTotal() + ""));
        return JsonResult.success(data);
    }

    @PostMapping("/load")
    public JsonResult load() {
        permissionService.load();
        return JsonResult.success();
    }

    @PostMapping("/loadPlus")
    public JsonResult loadPlus() {
        permissionService.load();
        return JsonResult.success();
    }

    @GetMapping("queryPermission/{id}")
    public JsonResult queryPermission(@PathVariable Long id) {
        List<Permission> permissions = permissionService.queryByPermission(id);
        return JsonResult.success(permissions);
    }
}
